package com.linkturing.swing;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import com.alibaba.fastjson.JSON;
import com.linkturing.util.BizHelper;
import com.linkturing.util.Constants;
import com.linkturing.util.JedisUtil;
import com.linkturing.util.JsonResult;
import com.linkturing.util.LoginResult;
import com.linkturing.util.User;

import cn.hutool.core.util.StrUtil;

public class ChangeLoginSwing extends JFrame {
	public ChangeLoginSwing() {
	}

	private JPanel contentPane;
	private JTextField txtXhadmin;

	private JFrame frame;
	private JLabel lblNewLabel_1;
	private JLabel lblNewLabel_2;
	private JPasswordField passwordField;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				try {
					LoginSwing frame = new LoginSwing("",null,"");
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ChangeLoginSwing(String date,JTextField mtextField,String juanhao) {
		frame = this;
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(500, 300, 458, 352);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		//张家港新华预应力钢绞线有限公司
		JLabel lblNewLabel = new JLabel("\u5F20\u5BB6\u6E2F\u65B0\u534E\u9884\u5E94\u529B\u94A2\u7EDE\u7EBF\u6709\u9650\u516C\u53F8");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 16));
		lblNewLabel.setForeground(SystemColor.textHighlight);
		lblNewLabel.setBounds(54, 34, 313, 40);
		contentPane.add(lblNewLabel);
		
		txtXhadmin = new JTextField();
		txtXhadmin.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				if(StrUtil.isBlank(txtXhadmin.getText())) {
					txtXhadmin.setText("请输入用户名");
				}
			}
		});
		txtXhadmin.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				txtXhadmin.setText("");
				if(StrUtil.isBlank(passwordField.getPassword().toString())) {
					passwordField.setText("请输入密码");
				}
			}
		});
		
		txtXhadmin.setForeground(SystemColor.activeCaption);
		txtXhadmin.setText("xhadmin");
		txtXhadmin.setBounds(114, 92, 269, 40);
		contentPane.add(txtXhadmin);
		txtXhadmin.setColumns(10);

		//确定
		JButton btnNewButton = new JButton("\u786E\u5B9A");
		btnNewButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {

				String passwd = new String(passwordField.getPassword());
				User user = new User();
				user.setUsername(txtXhadmin.getText());
				user.setPassword(passwd);
				LoginResult result = BizHelper.doLogin(user);
				if(result!=null&&result.isSuccess()) {
					JOptionPane.showMessageDialog(frame, "换班成功！");
					if(mtextField!=null) {
						mtextField.setText(txtXhadmin.getText());
					}
					frame.dispose();
				} else if(result!=null){
					JOptionPane.showMessageDialog(frame, result.getMessage());
				} else {
					JOptionPane.showMessageDialog(frame, "登录失败！");
				}
			}
		});
		btnNewButton.setBackground(SystemColor.textHighlight);
		btnNewButton.setBounds(84, 242, 316, 40);
		contentPane.add(btnNewButton);

		//密码
		lblNewLabel_1 = new JLabel("\u5BC6\u7801\uFF1A");
		lblNewLabel_1.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel_1.setBounds(40, 154, 64, 40);
		contentPane.add(lblNewLabel_1);

		//用户名：
		lblNewLabel_2 = new JLabel("\u7528\u6237\u540D\uFF1A");
		lblNewLabel_2.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel_2.setBounds(40, 92, 64, 40);
		contentPane.add(lblNewLabel_2);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(114, 155, 269, 40);
		contentPane.add(passwordField);
	}
	
	private boolean post(String date,String username) {
		Set<String> keys = JedisUtil.keys(date + ":weight:*");
		if(keys!=null&&keys.size()>0) {
			for(String key:keys) {
				List<String> jsonList = JedisUtil.getList(key);
				List<LtProductQualityBasic> pqbList = new ArrayList<LtProductQualityBasic>();
				List<LtProductQualityBasic> allPqbList = new ArrayList<LtProductQualityBasic>();
				if(jsonList!=null) {
					for(int i=0;i<jsonList.size();i++) {
		        		String json = jsonList.get(i);
		        		LtProductQualityBasic pqb = JSON.toJavaObject(JSON.parseObject(json), LtProductQualityBasic.class);
		        		if("0".equals(pqb.getSendFlag())) {
		        			pqbList.add(pqb);
		        		}
		        		allPqbList.add(pqb);
		        	}
					//提交
					String token = JedisUtil.get(Constants.user+username);
					if(pqbList.size()>0) {
						String result = BizHelper.postBatch(JSON.toJSONString(pqbList),token);
						System.out.println(result);
						handleResult(result,key,allPqbList);
					}
				}
			}
		}
		
		return true;
	}
	
	private void handleResult(String result,String key,List<LtProductQualityBasic> allPqbList) {
		JsonResult jr = JSON.toJavaObject(JSON.parseObject(result), JsonResult.class);
		if(jr!=null&&jr.isSuccess()) {
			List<LtProductQualityBasic> rsPqbList = jr.getResult();
			if(allPqbList!=null) {
				for(LtProductQualityBasic pqb:allPqbList) {
					if("1".equals(pqb.getSendFlag())) {
						continue;
					}
					if(isExitSuccess(pqb,rsPqbList)) {
						pqb.setSendFlag("1");
					}else {
						pqb.setSendFlag("2");
					}
				}
				//
				JedisUtil.resetList(key, allPqbList);
			}
		}
	}
	
	private boolean isExitSuccess(LtProductQualityBasic pqb,List<LtProductQualityBasic> rsPqbList) {
		if(rsPqbList!=null) {
			for(LtProductQualityBasic rsPqb:rsPqbList) {
				if(pqb.getVolumeNumber().equals(rsPqb.getVolumeNumber())&&pqb.getReelNumber().equals(rsPqb.getReelNumber())) {
					return rsPqb.isSuccess();
				}
			}
		}
		return false;
	}
}
